/**
 * 登录
 * 依赖于Ext.ux.js，hjp.utils.js
 * 如果登陆成功调用hjp.home.Main.js中的init方法
*/
hjp.home.Login = function() {
    return {
        loginUrl : '/login',
        loginFormConfig : {
            labelAlign : 'right',
            labelWidth : 60,
            frame : true,
            defaultType : 'textfield',
            monitorValid : true,
            items : [{
                xtype : 'Ext.ux.form.Spacer',
                height : 12
            },{
                fieldLabel : '用户名',
                name : 'username',
                tabIndex : 1,
                width : 150
            }, {
                fieldLabel : '密码',
                name : 'password',
                tabIndex : 2,
                inputType : 'password',
                width : 150
            },{
                xtype : 'checkbox',
                name : 'isPersistent',
                boxLabel: '在此计算机上保存我的信息。',
                labelSeparator: ''
            },{
                xtype : 'Ext.ux.form.Spacer',
                height : 12
            }]
        },
        init : function() {
            var mem = hjp.find('login_remember');
            if (mem && mem == "1") {
                this.autoLogin();
                return
            } //自动登录
            
            this.loginFormConfig.buttons = [{
                text : '登录',
                tabIndex : 3,
                formBind : true,
                scope : this,
                handler : this.loginHandler
            }]

            this.loginFormConfig.keys = {
                key : Ext.EventObject.ENTER,
                fn : this.loginFormConfig.buttons[0].handler,
                scope : this
            }

            this.loginForm = new Ext.form.FormPanel(this.loginFormConfig);
            this.loginWindow = new Ext.Window({
                title : '请登陆',
                closable : false, // 不允许关闭
                closeAction : 'hide',
                iconCls : 'icon-win',
                autoWidth : false,
                width : 310,
                autoHeight : true,
                modal : true,
                constrain : true,
                resizable : false,
                draggable : false,
                items : [this.loginForm]
            })

            this.loginWindow.on('show', function() {
                this.loginForm.find('name', 'username')[0].focus(true, 100);
            }, this)

            this.loginWindow.on('close', function() {
                this.loginForm.getForm().reset();
            }, this)

            this.loginForm.getForm().clearInvalid();
            this.loginWindow.show();
            
            hjp.utils.CapsLock.init();
        },
        clearRememberCookie : function() {
            hjp.clearRememberUserInfo();
        },
        loginHandler :function() {
            this.loginWindow.getEl().mask('','x-mask-loading');
            var username = this.loginForm.find('name', 'username')[0].getValue();
            var password = this.loginForm.find('name', 'password')[0].getValue();
            var remember = this.loginForm.find('name', 'isPersistent')[0].getValue();
            Ext.Ajax.request({
                scope : this,
                url : '/login',
                method : 'GET',
                params: {
                    'username': username,
                    'password': password
                },
                success : function(response, options) {
                    var resp = Ext.decode(response.responseText);
                    if (!resp.success) {
                        this.loginWindow.getEl().unmask();
                        Ext.Msg.alert('注意：', resp.errors.reason);
                        return;
                    }
                    this.loginWindow.hide();
                    this.loginForm.getForm().reset();

                    hjp.save("login_remember", remember ? "1" : "0")
                    if (remember) {
                        hjp.save("login_username", username)
                        hjp.save("login_password", password)
                    } else {
                        this.clearRememberCookie();
                    }
                    
                    this.loginWindow.getEl().unmask();
                    this.loginWindow.destroy();
                    hjp.home.Main.init();
                },
                failure : function(response, options) {
                    this.loginWindow.getEl().unmask();
                    Ext.Msg.alert('注意：', response.responseText);
                    this.loginForm.find('name', 'password')[0].focus(true)
                }
            }) //Ext.Ajax.request
        }, //loginHandler
        autoLogin : function() {
            var username = hjp.find('login_username');
            var password = hjp.find('login_password');

            Ext.Ajax.request({
                scope : this,
                url : '/login',
                method : 'GET',
                params: {
                    'username': username,
                    'password': password
                },                
                success : function(response, options) {
                    var resp = Ext.decode(response.responseText)
                    if (!resp.success || resp.passwordChanged) {
                        this.clearRememberCookie();
                        location.reload(false);
                        return;
                    }
                    hjp.home.Main.init();
                },
                failure : function(response, options) {
                    Ext.Msg.alert('注意：', response.responseText);
                    this.clearRememberCookie();
                    location.reload(false);
                }
            })
        }
    }
}()